/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cholm.media.castdr.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.cholm.media.castdr.LogUtils;
import org.cholm.media.castdr.R;
import org.cholm.media.castdr.backend.Category;
import org.cholm.media.castdr.backend.SubCategory;

import java.util.ArrayList;
import java.util.List;

import static org.cholm.media.castdr.LogUtils.LOGd;
// ===============================================================

/**
 * Adapter used to load the overview of sub-categories of a category
 *
 * @author cholm
 */
public class CategoryAdapter extends BaseAdapter {
    /**
     * Debug tag
     */
    private static final String TAG = LogUtils.makeLogTag(CategoryAdapter.class);
    /**
     * Category
     */
    private final Category mCategory;
    /**
     * Context
     */
    private Context mContext = null;
    /**
     * Holders
     */
    private List<Holder> mHolders = new ArrayList<Holder>();

    // =============================================================

    /**
     * Constructor
     *
     * @param ctx      Application context
     * @param category The category
     */
    public CategoryAdapter(Category category, Context ctx) {
        mCategory = category;
        mContext = ctx;
    }

    // ___________________________________________________________

    /**
     * Get our sub-category
     *
     * @param position Position
     * @return Sub-category or null
     */
    public SubCategory getSubCategory(int position) {
        return mCategory.getSub(position);
    }

    /**
     * Number of sub-categories
     */
    @Override
    public int getCount() {
        int n = mCategory.getMap().size();
        // LOGd(TAG, "Category " + mCategory.getName() + " has " + n
        //         + " children");
        return n;
    }

    /**
     * Get the item id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * Get view of sub
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final SubCategory s = getSubCategory(position);
        // LOGd(TAG,
        //         "Getting view for " + mCategory.getName() + "/" + s.getName());
        Holder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.subcategory_entry, null);
            holder = new Holder(convertView);
        }
        if (holder == null) holder = (Holder) convertView.getTag();
        holder.set(s);

        return convertView;
    }

    /**
     * Get the category object
     */
    @Override
    public Object getItem(int position) {
        return getSubCategory(position);
    }

    /**
     * Holder class
     */
    public class Holder {
        /**
         * Default background
         */
        public static final int DEFAULT_BG = Color.TRANSPARENT;
        private final String TAG = LogUtils
                .makeLogTag("CategoryAdapter.Holder");
         /**
         * Top view
         */
        public View mTopView = null;
        /**
         * Text view
         */
        public TextView mTextView = null;
        /**
         * Whether this is selected or not
         */
        public boolean mSelected = false;

        /**
         * Constructor
         *
         * @param v  View this is a holder for
         */
        public Holder(View v) {
            mTopView = v;
            mTextView = (TextView) v.findViewById(R.id.group_name);
            v.setTag(this);
            mHolders.add(this);
        }

        /*
         * (non-Javadoc)
         *
         * @see java.lang.Object#toString()
         */
        public String toString() {
            return "" + mTextView.getText();
        }

        /**
         * Update the display
         *
         * @param selected If true, show as selected
         */
        public void update(boolean selected) {
            LOGd(TAG, "Updating " + toString() + " "
                    + (selected ? "selected" : "deselected"));
            mSelected = selected;
            if (!mSelected) { // was selected
                mTopView.setBackgroundColor(DEFAULT_BG);
                mTextView.setTextColor(Color.BLACK);
            } else {
                mTopView.setBackgroundColor(mContext.getResources()
                        .getColor(R.color.drorange));
                mTextView.setTextColor(Color.WHITE);
            }
            if (!mSelected) return;
            for (Holder h : mHolders) {
                if (h == this) continue;
                h.update(false);
            }
        }

        /**
         * Set the UI components from object
         *
         * @param s SubCategory object
         */
        void set(SubCategory s) {
            mTextView.setText(s.getName());
            update(false);
        }
    }
}
// EOF
